﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using Database.Classes;


public partial class StoredProcedures
{
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void ColonizePlanet(int sId, int pId, int owner)
    {
        try
        {
            using (SqlConnection conn = new SqlConnection(StaticMethods.ConnectionString()))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Update Planets SET OwnerId= @s, PopulationLevel =1, PopToNextLevel = 21 Where SystemID = @sid and PlanetID = @pid", conn))
                {
                    cmd.Parameters.Add("@sid", SqlDbType.Int).Value = sId;
                    cmd.Parameters.Add("@pid", SqlDbType.Int).Value = pId;
                    cmd.Parameters.Add("@s", SqlDbType.Int).Value = owner;

                    cmd.ExecuteNonQuery();
                }
            }

            InsertNews(owner, DateTime.Now, "You have colonized " + UserDefinedFunctions.SystemName(sId).Value + " " + pId.ToString() + "(" + sId + " #" + pId + ")");
        }
        catch (Exception ex)
        {
            throw new Exception(sId.ToString() + " _ " + pId.ToString() + " _ " + owner.ToString(), ex);
        }
    }
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void RandomGet(int sId, int pId, int owner)
    {
        //TODO: add artifacts here
        Random r = new Random();
        int t = r.Next(1000);
        int bonus = 0;
        if (t < 100)
            bonus = r.Next(10, 150);
        else if (t < 200)
            bonus = r.Next(80, 200);
        else if (t < 300)
            bonus = r.Next(100, 500);
        else if (t < 400)
            bonus = r.Next(250, 500);
        else if (t < 500)
            bonus = r.Next(300, 600);
        else if (t < 900)
            bonus = r.Next(500, 1000);
        else if (t < 950)
            bonus = r.Next(800, 1000);
        else if (t < 990)
            bonus = r.Next(900, 1500);
        else if (t < 997)
            bonus = r.Next(1000, 2000);
        else bonus = r.Next(2000, 6000);
        AddCash(owner, bonus);
        InsertNews(pId, DateTime.Now, "You have found on the " + UserDefinedFunctions.SystemName(sId).Value + " #" + pId.ToString() + " $" + bonus.ToString() + ".");
    }
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void ConquerPlanet(int sId, int pId, int owner)
    {
        if (UserDefinedFunctions.PlanetOwner(sId, pId) == 1)
        {
            RandomGet(sId, pId, owner);

        }
        using (SqlConnection conn = new SqlConnection(StaticMethods.ConnectionString()))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("Update Planets SET OwnerId= @s, PopulationLevel =1, PopToNextLevel = 21 Where SystemID = @sid and PlanetID = @pid", conn))
            {
                cmd.Parameters.Add("@sid", SqlDbType.Int).Value = sId;
                cmd.Parameters.Add("@pid", SqlDbType.Int).Value = pId;
                cmd.Parameters.Add("@s", SqlDbType.Int).Value = owner;

                cmd.ExecuteNonQuery();
            }
        }
        InsertNews(owner, DateTime.Now, "You have conquered " + UserDefinedFunctions.SystemName(sId).Value + " " + pId.ToString() + "(" + sId + " #" + pId + ")");

    }
    private static void KillPopOnPlanet(int sId, int pId, int owner, ref int tr)
    {
        int pop = UserDefinedFunctions.PopulationOnPlanet(sId, pId);
        int newPop;
        if (tr < pop)
        {
            newPop = pop - tr;
            tr = 0;
        }
        else
        {
            newPop = 1;
            tr -= (pop - newPop);
        }
        using (SqlConnection conn = new SqlConnection(StaticMethods.ConnectionString()))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("Update Planets SET OwnerId= @s, PopulationLevel = @pl, PopToNextLevel = @pn Where SystemID = @sid and PlanetID = @pid", conn))
            {
                cmd.Parameters.Add("@sid", SqlDbType.Int).Value = sId;
                cmd.Parameters.Add("@pid", SqlDbType.Int).Value = pId;
                cmd.Parameters.Add("@s", SqlDbType.Int).Value = owner;
                cmd.Parameters.Add("@pl", SqlDbType.Int).Value = newPop;
                cmd.Parameters.Add("@pn", SqlDbType.Int).Value = Equations.PopulationLevel(newPop + 1);
                cmd.ExecuteNonQuery();
            }
        }
        if (pop - newPop != 0)
        {
            InsertNews(owner, DateTime.Now, "You have killed " + (pop - newPop).ToString() + " population on planet " + UserDefinedFunctions.SystemName(sId).Value + " " + pId.ToString() + "(" + sId + " #" + pId + ")");
            InsertNews(UserDefinedFunctions.PlanetOwner(sId, pId).Value, DateTime.Now, "You have lost " + (pop - newPop).ToString() + " population on planet " + UserDefinedFunctions.SystemName(sId).Value + " " + pId.ToString() + "(" + sId + " #" + pId + "). Killed by " + UserDefinedFunctions.PlayerName(owner));
        }
    }
};
